package com.lanou.controller;

import com.google.gson.Gson;
import com.lanou.service.SelectService;
import com.lanou.utils.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CategoryOneServlet",urlPatterns = "/onecate")
public class CategoryOneServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    private SelectService selectService = new SelectService();
    //通过id搜索单个类名
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Integer cid = Integer.parseInt(String.valueOf(session.getAttribute("cid")));
        System.out.println("onecate"+cid);
        response.setContentType("Application/json;charset=utf-8");
        AjaxResult ar = new AjaxResult();
        PrintWriter out = response.getWriter();
        String cname = selectService.selectCateById(cid);
        if (cname!=null){
            ar.setCode(1000);
            ar.setMsg("搜索成功");
            ar.setData(cname);
        }else {
            ar.setCode(1001);
            ar.setMsg("搜索失败");
        }
        out.write(new Gson().toJson(ar));
        out.close();
    }
}
